//Fdata_INC.js
var UserLanguageStr = "";

//1,1) MM/DD/YYYY;2,2) YYYY-MM-DD;3,3) DD.MM.YYYY
var UserDateFormat = 1;

//1,1) 12 Hour;2, 2) 24 Hour
var UserTimeFormat = 1;

//1,1) # (###) ###-####;2,2) AUS: (##) ####-####
var UserPhoneFormat = 1;

//Trim is not available for Excel, this adds it
if (typeof String.prototype.trim !== 'function') {
    String.prototype.trim = function () {
        return this.replace(/^\s+|\s+$/g, '');
    }
}

function FData(inputObj, dataType, length, required) {
    var value;
    if (inputObj.value != undefined) {
        value = inputObj.value;
    } else {
        value = inputObj;
    }
    var alt = "";
    var searchOp = "";
    /*if (UserLanguageStr == "") {
        UserLanguageStr = document.getElementById("UserLanguage").value;
        UserDateFormat = parseInt(document.getElementById("UserDateFormat").value);
        UserTimeFormat = parseInt(document.getElementById("UserTimeFormat").value);
        UserPhoneFormat = parseInt(document.getElementById("UserPhoneFormat").value);
        if (UserLanguageStr == "") {
            UserLanguageStr = "EN";
        }
    }*/
    try {
        searchOp = get_searchOp(value);
    } catch (e) {
    }

    if (value.length > 1) {
        value = value.replace(searchOp, "");
    }

    dataType = dataType.toLowerCase();

    if (searchOp.toUpperCase() == "NOT IS NULL" || searchOp.toUpperCase() == "IS NULL") {
        value = "";
    }
    var valueArray, i;
    switch (dataType) {
        case "currency":
            alt = 0;

            if (required || value != "") {
                if (searchOp == "><" && value.indexOf("^")) {
                    valueArray = value.split("^");

                    for (i = 0; i < valueArray.length; i++) {
                        valueArray[i] = formatCurrency(valueArray[i]);
                    }

                    value = valueArray.join("^");
                    value.replace(/^^/gi, "");

                    if (value == "^") {
                        value = "";
                    }
                } else {
                    value = formatCurrency(value);
                }
            }

            break;

        case "date":
            if (searchOp.toUpperCase() != "NOT IS NULL" && searchOp.toUpperCase() != "IS NULL") {
                if (searchOp == "><" && value.indexOf("^")) {
                    valueArray = value.split("^");

                    for (i = 0; i < valueArray.length; i++) {
                        valueArray[i] = fdate(valueArray[i], true);
                    }

                    value = valueArray.join("^");
                    value.replace(/^^/gi, "");

                    if (value == "^") {
                        value = "";
                    }
                } else {
                    value = fdate(value, true);
                }
            }

            break;

        case "datetime":
            if (searchOp == "><" && value.indexOf("^")) {
                valueArray = value.split("^");
                var tmpvalue = "";

                if (valueArray[0] && valueArray[0] != undefined && valueArray[0] != "") {
                    tmpvalue += fdatetime(valueArray[0]);
                }

                if (valueArray[1] && valueArray[1] != undefined && valueArray[1] != "") {
                    tmpvalue += "^" + fdatetime(valueArray[1]);
                }

                value = tmpvalue;
            } else {
                value = fdatetime(value);
            }

            break;

        case "datelocaltime":
            if (searchOp == "><" && value.indexOf("^")) {
                valueArray = value.split("^");

                for (i = 0; i < valueArray.length; i++) {
                    valueArray[i] = fdatetime(valueArray[i]);
                }

                value = valueArray.join("^");
                value.replace(/^^/gi, "");

                if (value == "^") {
                    value = "";
                }
            } else {
                value = fdatetime(value);
            }

            break;

        case "localtime":
            var IsPM = value.toUpperCase().indexOf("PM") > 0;

            if (value != "" && value.indexOf(":") < 0) {
                if (value.length < 3) {
                    value += ":00";
                } else if (value.length == 3) {
                    value = value.substr(0, 1) + ":" + value.substr(1, 3);
                } else {
                    value = value.substr(0, 2) + ":" + value.substr(2, 2);
                }
            }

            if (value != "" && IsPM && value.indexOf("PM") < 0) {
                value += " PM";
            }

            var TimeComponents = [];
            TimeComponents = value.split(" ");

            var Time = TimeComponents[0];
            var IsPm = TimeComponents.length > 1 && TimeComponents[1] == "PM";
            var HoursMinutes = [];
            HoursMinutes = Time.split(":");

            switch (UserTimeFormat) {
                case 2:
                    if (IsPm) {
                        value = (parseInt(HoursMinutes[0]) + 12) + ":" + HoursMinutes[1];
                    } else {
                        value = HoursMinutes[0] + ":" + HoursMinutes[1];
                    }

                    break;

                default:
                    if (IsPm) {
                        value = HoursMinutes[0] + ":" + HoursMinutes[1] + " PM";
                    } else {
                        value = HoursMinutes[0] + ":" + HoursMinutes[1] + " AM";
                    }

                    break;
            }

            break;

        case "monthday":
            if (value != "" && value.indexOf("/") < 0) {
                value += "2000";
            }

            if (value != "" && value.indexOf("/") >= 0) {
                value += "/2000";
            }

            value = fdate(value, true);
            var tmpArr = value.split("/");
            (tmpArr.length > 1) ? value = tmpArr[0] + "/" + tmpArr[1] : value = "";
            break;

        case "integer":
            alt = 0;

            if (searchOp == "><" && value.indexOf("^")) {
                valueArray = value.split("^");

                for (i = 0; i < valueArray.length; i++) {
                    if (valueArray[i] == "" || /^[\d|-]+$/.test(valueArray[i])) {
                        valueArray[i] = cnumber(valueArray[i]);
                    } else {
                        valid = false;
                        valueArray[i] = "";
                    }
                }

                value = valueArray.join("^");
                value.replace(/^^/gi, "");

                if (value == "^") {
                    value = "";
                }
            } else {
                if (value == "" || /^[\d|-]+$/.test(value)) {
                    value = cnumber(value);
                } else {
                    value = "";
                }
            }

            break;

        case "decimal":
            alt = 0;

            if (searchOp == "><" && value.indexOf("^")) {
                valueArray = value.split("^");

                for (i = 0; i < valueArray.length; i++) {
                    if (valueArray[i] == "" || /^[\d|.|-]+$/.test(valueArray[i])) {
                        valueArray[i] = cnumber(valueArray[i]);
                    } else {
                        valueArray[i] = "";
                    }
                }

                value = valueArray.join("^");
                value.replace(/^^/gi, "");

                if (value == "^") {
                    value = "";
                }
            } else {
                if (value == "" || /^[\d|.|-]+$/.test(value)) {
                    value = cnumber(value);
                } else {
                    value = "";
                }
            }

            break;

        case "color":
        case "textnospc":
            value = Replace(value, " ", "");
            value = Replace(value, "&", "");
            value = Replace(value, "'", "");

        case "text":
            value = Replace(value, "  ", " ");

        case "textdblspc":
            value = Replace(value, "^", "&#94;");
            value = Replace(value, "|", "&#124;");
            value = Replace(value, "~", "&#126;");
            value = Replace(value, "\"", "");
            value = value.toString().substring(0, length);
            break;

        case "textencoded":
            break;

        case "autonumber":
            break;

        case "email":
            if (value != "" && !IsEmail(value)) {
                alert(value + " is not a valid email. If must be in the form or 'username@domain.com'.");
                value = ""
            }

            break;

        case "phone":
            value = value.toString();
            value = Replace(value, "(", "");
            value = Replace(value, ")", "");
            value = Replace(value, "-", "");
            value = Replace(value, " ", "");
            value = Replace(value, ".", "");

            if (!isNaN(parseFloat(value))) {
                switch (value.length) {
                    case 11:
                        value = value.substring(0, 1) + " (" + value.substring(1, 4) + ") " + value.substring(4, 7) + "-" + value.substring(7, 11);
                        break;

                    case 10:
                        switch (UserPhoneFormat) {
                            case 2:
                                if (value.substring(0, 2) == "04") {
                                    value = "" + value.substring(0, 4) + " " + value.substring(4, 7) + "-" + value.substring(7, 10);
                                } else {
                                    value = "(" + value.substring(0, 2) + ") " + value.substring(2, 6) + "-" + value.substring(6, 10);
                                }

                                break;

                            default:
                                value = "(" + value.substring(0, 3) + ") " + value.substring(3, 6) + "-" + value.substring(6, 10);
                                break;
                        }

                        break;

                    case 7:
                        value = value.substring(0, 3) + "-" + value.substring(3, 7);
                        break;

                    default:
                        value = inputObj.value.replace(searchOp, "");
                }
            }

            break;

        case "postalcode":
            value = value.toString();
            value = Replace(value, "-", "");
            value = Replace(value, " ", "");

            if (!isNaN(parseFloat(value))) {
                switch (value.length) {
                    case 9:
                        value = value.substring(0, 5) + "-" + value.substring(5, 9);
                        break;
                    case 4:
                    case 5: //AUS has 4 digit postal code
                        value = value;
                        break;
                    default:
                        value = inputObj.value.replace(searchOp, "");
                }
            } else {
                //Canada
                switch (value.length) {
                    case 6:
                        value = value.substring(0, 3) + " " + value.substring(3, 6);
                        break;
                    default:
                        value = inputObj.value.replace(searchOp, "");
                }
            }

            break;
        case "upc":
            value = FormatUPC(value);
            break;

        default:
            alert("FData() Invalid Datatype (" + dataType + ").");
    }

    if ((value + "") == "") {
        if (required) {
            alert("这是必填字段. 它必须包含一个有效的价值.");
            value = alt;
        }
    }

    if (inputObj.value != undefined) {
        if (inputObj.value.length > 1) {
            if (searchOp == "><" && value.indexOf("^") <= -1) {
                inputObj.value = ">" + value;
            } else {
                inputObj.value = searchOp + value;
            }
        } else {
            inputObj.value = value;
        }
    } else {
        return value;
    }
}

function IsEmail(email) {
    var regex = /^([a-zA-Z0-9_.+-])+\@(([a-zA-Z0-9-])+\.)+([a-zA-Z0-9]{2,4})+$/;
    return regex.test(email);
}

function FormatUPC(upc) {
    var A = "";
    var B = "";
    var C = "";
    var D = "";
    var E = "";
    upc = Replace(upc, " ", "");
    upc = Replace(upc, "-", "");

    if (isNaN(parseFloat(upc))) {
        return upc;
    }

    switch (upc.length) {
        case 12:
            A = upc.substring(0, 1);
            B = upc.substring(1, 6);
            C = upc.substring(6, 11);
            D = upc.substring(11, 12);
            upc = A + "-" + B + "-" + C + "-" + D;
            break;

        case 11:
            A = upc.substring(0, 1);
            B = upc.substring(1, 6);
            C = upc.substring(6, 11);
            upc = A + "-" + B + "-" + C;
            break;

        case 10:
            A = "";
            B = upc.substring(0, 5);
            C = upc.substring(5, 10);
            D = "";
            upc = B + "-" + C;
            break;
        case 14: // Task 92519: 14 digit UPC/GTIN with check digit and dashes wanted in the unit and carrier UPC field for products
            A = upc.substring(0, 1);
            B = upc.substring(1, 3);
            C = upc.substring(3, 8);
            D = upc.substring(8, 13);
            E = upc.substring(13, 14);
            upc = A + "-" + B + "-" + C + "-" + D + "-" + E;
            break;
    }

    return upc;
}

function cnumber(Value) {
    if (Value == "") {
        return "";
    } else {
        Value = parseFloat(Value);

        if (isNaN(Value)) {
            alert(Value + " is not a number.");
            return "";
        } else {
            return Value;
        }
    }
}

function formatCurrency(num) {
    var currencySynmbol = "$";
    num = num.toString().replace(/\$|\,/g, "");

    if (isNaN(num)) {
        num = "0";
    }

    sign = (num == (num = Math.abs(num)));
    num = Math.floor(num * 1000000 + 0.50000000001);
    cents = (num % 1000000) / 1000000;
    num = Math.floor(num / 1000000).toString();
    cents = cents.toString();
    cents = Replace(cents, "0.", "");

    if (cents.toString().length < 2) {
        cents += "0";
    }

    if (UserLanguageStr == "") {
        UserLanguageStr = document.getElementById("UserLanguage").value;
        UserDateFormat = parseInt(document.getElementById("UserDateFormat").value);
        UserTimeFormat = parseInt(document.getElementById("UserTimeFormat").value);
        if (UserLanguageStr == "") {
            UserLanguageStr = "EN";
        }
    }
    if (UserLanguageStr == "ZH") {
        currencySynmbol = String.fromCharCode(165);
    }

    return (((sign) ? "" : "(") + currencySynmbol + num + "." + cents + ((sign) ? "" : ")"));
}

var getType = function (obj) {
    return ({}).toString.call(obj).match(/\s([a-zA-Z]+)/)[1].toLowerCase();
};

function DateParseFormat(value, nonVerbose) {
    var myMonth = "";
    var myDate = "";
    var myYear = "";
    var dateElemntArr = null;
    var tmp = value;

    tmp = tmp.replace("\\", "/");

    if (tmp.indexOf(".", 0) != -1 && UserDateFormat == 3) {
        dateElemntArr = tmp.split(".");

        if (dateElemntArr.length > 2) {
            myMonth = dateElemntArr[1];
            myDate = dateElemntArr[0];
            myYear = dateElemntArr[2];

            tmp = myMonth + "/" + myDate + "/" + myYear;
        } else if (dateElemntArr.length == 2) {
            myMonth = dateElemntArr[1];
            myDate = dateElemntArr[0];
            tmp = myMonth + "/" + myDate;
        }
    } else if (tmp.indexOf("-", 0) != -1 && (tmp.split("-")[0].length == 4 || UserDateFormat == 2)) {
        dateElemntArr = tmp.split("-");

        if (dateElemntArr.length > 2) {
            myMonth = dateElemntArr[1];
            myDate = dateElemntArr[2];
            myYear = dateElemntArr[0];

            tmp = myMonth + "/" + myDate + "/" + myYear;
        } else if (dateElemntArr.length == 2) {
            myMonth = dateElemntArr[0];
            myDate = dateElemntArr[1];
            tmp = myMonth + "/" + myDate;
        }
    } else {
        tmp = tmp.replace(/-/gi, "/").replace(/\./gi, "/");
    }

    if (tmp.toString().indexOf("/") == -1) {
        if (tmp.length > 8) {
            tmp = tmp.substring(0, 8);
        }

        switch (tmp.length) {
            case 3:
                myMonth = tmp.substr(0, 1);
                myDate = tmp.substr(1, 2);
                break;
            case 4:
                myMonth = tmp.substr(0, 2);
                myDate = tmp.substr(2, 2);
                break;
            case 5:
                myMonth = tmp.substr(0, 1);
                myDate = tmp.substr(1, 2);
                myYear = tmp.substr(4, 2);
                break;
            case 6:
                myMonth = tmp.substr(0, 2);
                myDate = tmp.substr(2, 2);
                myYear = tmp.substr(4, 2);
                break;
            case 7:
                myMonth = tmp.substr(0, 1);
                myDate = tmp.substr(1, 2);
                myYear = tmp.substr(5, 4);
                break;
            case 8:
                myMonth = tmp.substr(0, 2);
                myDate = tmp.substr(2, 2);
                myYear = tmp.substr(5, 4);
                break;
        }
    } else {
        var tmpArr = tmp.split("/");
        myMonth = tmpArr[0];

        if (tmpArr.length > 1) {
            myDate = tmpArr[1];
        }

        if (tmpArr.length > 2) {
            myYear = tmpArr[2];
        }
    }

    if (myYear.length < 4 && myYear != "") {
        myYear = parseFloat(myYear) + 2000;
    }

    myMonth = parseFloat(myMonth);
    myDate = parseFloat(myDate);
    myYear = parseFloat(myYear);
    var myDateObj = new Date();

    if (isNaN(myMonth) || myMonth < 1 || myMonth > 12) {
        if (!nonVerbose) {
            alert("Invalid month (" + myMonth + "), defaulting to this month");
        }

        myMonth = myDateObj.getMonth() + 1;
    }

    if (isNaN(myDate) || myDate < 1) {
        myDate = 1;
    }

    if (isNaN(myDate) || myDate > 31) {
        myDate = 31;
    }

    if (isNaN(myYear) || myYear < 1950 || myYear > 2050) {
        myYear = myDateObj.getFullYear();
    }

    switch (myMonth) {
        case 4:
        case 6:
        case 9:
        case 11:
            if (myDate > 30) {
                myDate = 30;
            }

            break;

        case 2:
            var daysInFeb = new Date(myYear, 2, 0).getDate();

            if (myDate > daysInFeb) {
                myDate = daysInFeb;
            }
    }

    return myMonth + "/" + myDate + "/" + myYear;
}

function fdate(value, nonVerbose) {
    if (getType(value) == "date") {
        value = (value.getMonth() + 1 + "/" + value.getDate() + "/" + value.getFullYear());
    }

    var tmp = value.toString().replace(/^\s+|\s+$/g, "");

    if (tmp == "") {
        return tmp;
    }

    var tmpDateArr = DateParseFormat(tmp, nonVerbose).split("/");
    var myMonth = tmpDateArr[0];
    var myDate = tmpDateArr[1];
    var myYear = tmpDateArr[2];

    switch (UserDateFormat) {
        case 2:
            return myYear + "-" + myMonth + "-" + myDate;
            break;

        case 3:
            return myDate + "." + myMonth + "." + myYear;
            break;

        default:
            return myMonth + "/" + myDate + "/" + myYear;
            break;
    }
}

function fdatetime(value) {
    var tmp = value.trim();
    var tmpArr = tmp.split(" ");
    var myHour = "";
    var myMinute = "";
    var mySecond = "";
    var myAMPM = "";

    if (tmpArr.length > 2) {
        myAMPM = tmpArr[2].toUpperCase();

        if (myAMPM != "AM" && myAMPM != "PM") {
            myAMPM = "";
        }
    }

    if (tmpArr.length > 1) {
        tmpArr = tmpArr[1].split(":");

        if (tmpArr.length > 1) {
            myHour = tmpArr[0];
            myMinute = tmpArr[1];
        }

        if (tmpArr.length > 2) {
            mySecond = tmpArr[2];
        }
    }

    myHour = parseFloat(myHour);
    myMinute = parseFloat(myMinute);
    mySecond = parseFloat(mySecond);

    if (isNaN(myHour)) {
        myHour = "0";
    }

    if (isNaN(myMinute)) {
        myMinute = "0";
    }

    if (isNaN(mySecond)) {
        mySecond = "0";
    }

    myHour = parseFloat(myHour);
    myMinute = parseFloat(myMinute);
    mySecond = parseFloat(mySecond);

    if (myHour < 0 || myHour > 24) {
        myHour = 0;
    }

    if (myMinute < 0 || myMinute > 60) {
        myMinute = 0;
    }

    if (mySecond < 0 || mySecond > 60) {
        mySecond = 0;
    }

    myHour = "0" + myHour;
    myMinute = "0" + myMinute;
    mySecond = "0" + mySecond;

    myHour = myHour.substr(myHour.length - 2, myHour.length);
    myMinute = myMinute.substr(myMinute.length - 2, myMinute.length);
    mySecond = mySecond.substr(mySecond.length - 2, mySecond.length);

    var tmp2 = myHour + ":" + myMinute;

    if (mySecond != "00") {
        tmp2 += ":" + mySecond;
    }

    tmp = fdate(value, true);

    if (tmp2 != "00:00") {
        if (myAMPM != "") {
            tmp2 += " " + myAMPM;
        }
        tmp += " " + tmp2;
    }

    return tmp;
}

function CDec(Value) {
    Value = parseFloat(Value);

    if (isNaN(Value)) {
        Value = 0;
    }

    return Value;
}

function ABS(Value) {
    Value = CDec(Value);
    Value = Math.abs(Value);

    return Value;
}

function Left(Value, Length) {
    var result;
    if (Length <= 0) {
        result = "";
    } else if (Length > String(Value).length) {
        result = Value;
    } else {
        result = String(Value).substring(0, Length);
    }
    return result;
}

function Right(Value, Length) {
    var result;
    if (Length <= 0) {
        result = "";
    } else if (Length > String(Value).length) {
        result = Value;
    } else {
        var StringLength = String(Value).length;
        result = String(Value).substring(StringLength, StringLength - Length);
    }
    return result;
}